﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Entities;
using Microsoft.AspNetCore.Mvc;

namespace Bot.WebAPI.Controllers
{
    [Route("api/v3/chatbot/bots")]
    [ApiController]
    public class ChatbotsController : ControllerBase
    {

        private readonly IChatbotAppService _app;
        public ChatbotsController(IChatbotAppService app)
        {
            this._app = app;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="include">Available value: intent</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<ChatbotDto> Get([FromRoute] Guid id, [FromQuery]string include)
        {
            ChatbotDto chatbot = _app.Get(id, include);
            return Ok(chatbot);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="include">Available value: greetingMessageInChannels,noAnswerMessageInChannels, messageAfterSeveralConsecutivePossibleAnswersInChannels</param>
        /// <param name="channel"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<IEnumerable<ChatbotDto>> GetList([FromQuery] string include, [FromQuery] string channel)
        {
            IEnumerable<ChatbotDto> list = _app.GetList(include, channel);
            return Ok(list);
        }

        [HttpPut]
        [Route("{id:Guid}")]
        public ActionResult<ChatbotDto> Put([FromRoute] Guid id, [FromBody] ChatbotUpdateDto dto)
        {
            ChatbotDto chatbot = _app.Update(id,dto);
            return Ok(chatbot);
        }

        [HttpPost]
        public ActionResult<ChatbotDto> Post([FromBody] ChatbotCreateDto dto, [FromQuery] bool ifDefault, [FromQuery] string projectId)
        {
            ChatbotDto chatbot = _app.Add(dto, ifDefault, projectId);
            return CreatedAtAction(nameof(Get), new { id = chatbot.Id }, chatbot);
        }

        [HttpDelete("{id}")]
        public ActionResult Delete([FromRoute] Guid id)
        {
            _app.Delete(id);
            return NoContent();
        }

        [HttpGet]
        [Route("{id:Guid}:export")]
        public ActionResult<string> Export([FromRoute] Guid id)
        {
            var url = _app.Export(id);
            return Ok(new { fileUrl = url });
        }

        [HttpPost]
        [Route("{id:Guid}:import")]
        public ActionResult<OperationDto> Import([FromRoute] Guid id)
        {
            string filePath = "";
            OperationDto operation = _app.Import(id, filePath);
            return Ok(operation);
        }

        [HttpPost]
        [Route("{id:Guid}:train")]
        public ActionResult<OperationDto> Train([FromRoute] Guid id)
        {
            OperationDto operation = _app.Train(id);
            return Ok(operation);
        }

        [HttpPost]
        [Route("{id:Guid}:test")]
        public ActionResult<TestBotResponseDto> Test([FromRoute] Guid id,[FromBody] TestRequestBodyDto testRequestBodyDto)
        {
            TestBotResponseDto testBotResponseDto = _app.Test(id, testRequestBodyDto);
            return Ok(testBotResponseDto);
        }

    }
}
